//
//  CJBaseController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-04-14.
//

import UIKit

open class CJBaseController: UIViewController, UIGestureRecognizerDelegate {

    public var cj_data: Any?
    
    /// 导航栏状态 (子类viewDidLoad中设置)
    public var cj_navBarStyle: CJNavBarStyle = .plain
    
    public var cj_navBarBackColor: UIColor?
    public var cj_navBarTintColor: UIColor?
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        extendedLayoutIncludesOpaqueBars = true
        edgesForExtendedLayout = .all
        if #available(iOS 11.0, *) {
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        navigationController?.interactivePopGestureRecognizer?.delegate = self
        
        view.cj_didSkin = { [weak self] kit in
            self?.cj_skinAction(kit)
        }
    }
    
    /// 全局项目刷新回调(子类继承实现)
    open func cj_skinAction(_ kit: Any) {
        view.backgroundColor = GV_CJFrame_backColor
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setupNavBarStyle()
        setupNavBarDefaultApperance()
        
        if isBeingPresented || isMovingToParent {
            // push/present
        } else {
            // pop/dismiss to here
        }
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        if isBeingDismissed || isMovingFromParent {
            // pop/dismiss
        } else {
            // push/present from here
        }
    }
    
    /// 设置导航栏状态
    open func setupNavBarStyle() {
        if cj_navBarStyle == .hidden {
            navigationController?.setNavigationBarHidden(true, animated: true)
        } else if cj_navBarStyle == .show {
            navigationController?.setNavigationBarHidden(false, animated: true)
        } else if cj_navBarStyle == .plain {
            navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }
    
    /// 更新导航栏颜色
    open func setupNavBarDefaultApperance() {
        let backColor = cj_navBarBackColor ?? GV_CJFrame_navBarBackColor
        let tintColor = cj_navBarTintColor ?? GV_CJFrame_navBarTintColor
        setupNavBarApperance(backColor: backColor, tintColor: tintColor)
    }
    
    /// 设置导航栏颜色(子类viewWillAppear中设置)
    open func setupNavBarApperance(backColor: UIColor = .white,
                              tintColor: UIColor = .black) {
        
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundColor = backColor
            appearance.shadowColor = UIColor.clear
            appearance.titleTextAttributes = [.foregroundColor: tintColor]
            navigationController?.navigationBar.standardAppearance = appearance
            navigationController?.navigationBar.scrollEdgeAppearance = appearance
        } else {
            navigationController?.navigationBar.isTranslucent = false
            navigationController?.navigationBar.barTintColor = backColor
            navigationController?.navigationBar.tintColor = tintColor
            navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: tintColor]
            
            navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
            navigationController?.navigationBar.shadowImage = UIImage()
        }
    }
    
    deinit {
        kCJLog("\(self)--deinit")
    }

}

// MARK: 返回控制器
/// 返回控制器
open class CJBaseBackController: CJBaseController {
    
    open var cj_backBarItem: UIBarButtonItem?
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        setupBackBarItem()
    }
    
    open override func cj_skinAction(_ kit: Any) {
        super.cj_skinAction(kit)
        
        if let backBarItemImage = GV_CJFrame_backBarItemImage {
            cj_backBarItem?.image = backBarItemImage;
        }
    }

    /// 设置返回barItem
    open func setupBackBarItem() {
        if let backBarItemImage = GV_CJFrame_backBarItemImage {
            cj_backBarItem = UIBarButtonItem(image: backBarItemImage,
                                             style: .plain,
                                             target: self,
                                             action: #selector(cj_backBarItemAction))
        }
        if let cj_backBarItem = cj_backBarItem {
            navigationItem.leftBarButtonItem = cj_backBarItem
        }
    }
    
    /// 返回barItem实现方法
    @objc open func cj_backBarItemAction() {
        navigationController?.popViewController(animated: true)
    }
}

// MARK: 菜单控制器
/// 菜单控制器
open class CJBaseMenuController: CJBaseController {
    
    open override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // 禁用返回手势
        let respond = navigationController?.responds(to: NSSelectorFromString("interactivePopGestureRecognizer"))
        if let respond = respond {
            if respond == true {
                navigationController?.interactivePopGestureRecognizer?.isEnabled = false
            }
        }
    }
    
    open override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        // 启用返回手势
        let respond = navigationController?.responds(to: NSSelectorFromString("interactivePopGestureRecognizer"))
        if let respond = respond {
            if respond == true {
                navigationController?.interactivePopGestureRecognizer?.isEnabled = true
            }
        }
    }
}
